# make dates numeric for following analyses
ESS6$date <- as.numeric(ESS6$date)
ESS7$date <- as.numeric(ESS7$date)
ESS8$date <- as.numeric(ESS8$date)
ESS9$date <- as.numeric(ESS9$date)
ESS10$date <- as.numeric(ESS10$date)
par(mfrow = c(1,1))


### ESS 6 variables ###

# recode variables

# imdfetn -> high values = "Allow fewer migrants of different race"
table(ESS6$imdfetn)
ESS6$race <- ESS6$imdfetn 
ESS6$race[ESS6$race == 7 | ESS6$race == 8 | ESS6$race == 9] <- NA
table(ESS6$race)

# impcntr -> high values = "Allow fewer immigrants from outside Europe"
table(ESS6$impcntr)
ESS6$oute <- ESS6$impcntr 
ESS6$oute[ESS6$oute == 7 | ESS6$oute == 8 | ESS6$oute == 9] <- NA
table(ESS6$oute)

# imwbcnt -> high values = "Immigrants make country better"
table(ESS6$imwbcnt)
ESS6$mig <- ESS6$imwbcnt 
ESS6$mig[ESS6$mig == 77 | ESS6$mig == 88 | ESS6$mig == 99] <- NA
table(ESS6$mig)

# imueclt -> high values = "Cultural life enriched by migrants"
table(ESS6$imueclt)
ESS6$cul <- ESS6$imueclt
ESS6$cul[ESS6$cul == 77 | ESS6$cul == 88 | ESS6$cul == 99] <- NA
table(ESS6$cul)

# imbgeco -> high values = "Immigration good for economy"
table(ESS6$imbgeco)
ESS6$eco <- ESS6$imbgeco
ESS6$eco[ESS6$eco == 77 | ESS6$eco == 88 | ESS6$eco == 99] <- NA
table(ESS6$eco)

# imsmetn -> high values = allow fewer migrants of majority ethnicity
table(ESS6$imsmetn)
ESS6$ins <- ESS6$imsmetn
ESS6$ins[ESS6$ins == 7 | ESS6$ins == 8 | ESS6$ins == 9] <- NA
table(ESS6$ins)

# lrscale 0 = left, 10 = right
table(ESS6$lrscale)
ESS6$lrscale[ESS6$lrscale == 77 | ESS6$lrscale == 88 | ESS6$lrscale == 99] <- NA
table(ESS6$lrscale)

# delete NAs for work hours
ESS6$wkhtot[ESS6$wkhtot == 666 | ESS6$wkhtot == 777 | ESS6$wkhtot == 888 | ESS6$wkhtot == 999] <- NA
ESS6$wkhtot <- as.numeric(ESS6$wkhtot)
table(ESS6$wkhtot)

# delete NAs for state of education
ESS6$stfedu[ESS6$stfedu == 77 | ESS6$stfedu == 88 | ESS6$stfedu == 99] <- NA
ESS6$stfedu <- as.numeric(ESS6$stfedu)
table(ESS6$stfedu)

# delete NAs for state of health services
ESS6$stfhlth[ESS6$stfhlth == 77 | ESS6$stfhlth == 88 | ESS6$stfhlth == 99] <- NA
ESS6$stfhlth <- as.numeric(ESS6$stfhlth)
table(ESS6$stfhlth)

# delete NAs for gayrights
ESS6$freehms[ESS6$freehms == 7 | ESS6$freehms == 8 | ESS6$freehms == 9] <- NA
ESS6$freehms <- as.numeric(ESS6$freehms)
table(ESS6$freehms)


# covariates

# education
ESS6$education <- NA
ESS6$education[ESS6$edulvlb < 120] <- "Primary"
ESS6$education[ESS6$edulvlb > 120 & ESS6$edulvlb < 500] <- "Secondary"
ESS6$education[ESS6$edulvlb >500 & ESS6$edulvlb < 1000] <- "Tertiary"

#gndr
ESS6$gndr[ESS6$gndr > 2] <- NA

#agea
ESS6$agea[ESS6$agea > 110] <- NA

#foreignparent
ESS6$foreignparent <- NA
ESS6$foreignparent[ESS6$mocntr == 2 | ESS6$facntr == 2] <- "Foreign Parent"
ESS6$foreignparent[ESS6$mocntr == 1 & ESS6$facntr == 1] <- "Two Native Parents"

#workstat
ESS6$workstat <- NA
ESS6$workstat[ESS6$mainact == 1] <- "Paid Work"
ESS6$workstat[ESS6$mainact == 2] <- "Education"
ESS6$workstat[ESS6$mainact == 3 | ESS6$mainact == 4] <- "Unemployed"
ESS6$workstat[ESS6$mainact == 6] <- "Retired"
ESS6$workstat[ESS6$mainact == 5 | ESS6$mainact == 7 | ESS6$mainact == 8 | ESS6$mainact == 9] <- "Housework or Other"

### create factors ###
fact61 <- prcomp(~ESS6$mig + ESS6$cul + ESS6$eco)
fact61
f61 <- get_pca_ind(fact61)
f61$coord
data61 <- ESS6 %>% 
  filter(mig >-1 & eco > -1 & cul > -1) 
data61$factor <- -1 * f61$coord[,1]
summary(data61$factor)

fact62 <- prcomp(~ESS6$oute + ESS6$race + ESS6$ins)
fact62
f62 <- get_pca_ind(fact62)
f62$coord
data62 <- ESS6 %>% 
  filter(oute >-1 & race > -1 & ins > -1) 
data62$factor <- f62$coord[,1]
summary(data62$factor)



### ESS 7 variables ###

# imdfetn -> high values = "Allow fewer migrants of different race"
table(ESS7$imdfetn)
ESS7$race <- ESS7$imdfetn 
ESS7$race[ESS7$race == 7 | ESS7$race == 8 | ESS7$race == 9] <- NA
table(ESS7$race)

# impcntr -> high values = "Allow fewer immigrants from outside Europe"
table(ESS7$impcntr)
ESS7$oute <- ESS7$impcntr 
ESS7$oute[ESS7$oute == 7 | ESS7$oute == 8 | ESS7$oute == 9] <- NA
table(ESS7$oute)

# imwbcnt -> high values = "Immigrants make country better"
table(ESS7$imwbcnt)
ESS7$mig <- ESS7$imwbcnt 
ESS7$mig[ESS7$mig == 77 | ESS7$mig == 88 | ESS7$mig == 99] <- NA
table(ESS7$mig)

# imueclt -> high values = "Cultural life enriched by migrants"
table(ESS7$imueclt)
ESS7$cul <- ESS7$imueclt
ESS7$cul[ESS7$cul == 77 | ESS7$cul == 88 | ESS7$cul == 99] <- NA
table(ESS7$cul)

# imbgeco -> high values = "Immigration good for economy"
table(ESS7$imbgeco)
ESS7$eco <- ESS7$imbgeco
ESS7$eco[ESS7$eco == 77 | ESS7$eco == 88 | ESS7$eco == 99] <- NA
table(ESS7$eco)

# imsmetn -> high values = allow fewer migrants of majority ethnicity
table(ESS7$imsmetn)
ESS7$ins <- ESS7$imsmetn
ESS7$ins[ESS7$ins == 7 | ESS7$ins == 8 | ESS7$ins == 9] <- NA
table(ESS7$ins)

# delete NAs for work hours
ESS7$wkhtot[ESS7$wkhtot == 666 | ESS7$wkhtot == 777 | ESS7$wkhtot == 888 | ESS7$wkhtot == 999] <- NA
ESS7$wkhtot <- as.numeric(ESS7$wkhtot)
table(ESS7$wkhtot)

# delete NAs for state of education
ESS7$stfedu[ESS7$stfedu == 77 | ESS7$stfedu == 88 | ESS7$stfedu == 99] <- NA
ESS7$stfedu <- as.numeric(ESS7$stfedu)
table(ESS7$stfedu)

# delete NAs for state of health services
ESS7$stfhlth[ESS7$stfhlth == 77 | ESS7$stfhlth == 88 | ESS7$stfhlth == 99] <- NA
ESS7$stfhlth <- as.numeric(ESS7$stfhlth)
table(ESS7$stfhlth)

# delete NAs for gayrights
ESS7$freehms[ESS7$freehms == 7 | ESS7$freehms == 8 | ESS7$freehms == 9] <- NA
ESS7$freehms <- as.numeric(ESS7$freehms)
table(ESS7$freehms)


# lrscale 0 = left, 10 = right
table(ESS7$lrscale)
ESS7$lrscale[ESS7$lrscale == 77 | ESS7$lrscale == 88 | ESS7$lrscale == 99] <- NA
table(ESS7$lrscale)



# covariates

# education
ESS7$education <- NA
ESS7$education[ESS7$edulvlb < 120] <- "Primary"
ESS7$education[ESS7$edulvlb > 120 & ESS7$edulvlb < 500] <- "Secondary"
ESS7$education[ESS7$edulvlb >500 & ESS7$edulvlb < 1000] <- "Tertiary"

#gndr
ESS7$gndr[ESS7$gndr > 2] <- NA

#agea
ESS7$agea[ESS7$agea > 110] <- NA

#foreignparent
ESS7$foreignparent <- NA
ESS7$foreignparent[ESS7$mocntr == 2 | ESS7$facntr == 2] <- "Foreign Parent"
ESS7$foreignparent[ESS7$mocntr == 1 & ESS7$facntr == 1] <- "Two Native Parents"

#workstat
ESS7$workstat <- NA
ESS7$workstat[ESS7$mainact == 1] <- "Paid Work"
ESS7$workstat[ESS7$mainact == 2] <- "Education"
ESS7$workstat[ESS7$mainact == 3 | ESS7$mainact == 4] <- "Unemployed"
ESS7$workstat[ESS7$mainact == 6] <- "Retired"
ESS7$workstat[ESS7$mainact == 5 | ESS7$mainact == 7 | ESS7$mainact == 8 | ESS7$mainact == 9] <- "Housework or Other"


### create factors ###
fact71 <- prcomp(~ESS7$mig + ESS7$cul + ESS7$eco)
fact71
f71 <- get_pca_ind(fact71)
f71$coord
data71 <- ESS7 %>% 
  filter(mig >-1 & eco > -1 & cul > -1) 
data71$factor <- f71$coord[,1]
summary(data71$factor)

fact72 <- prcomp(~ESS7$oute + ESS7$race + ESS7$ins)
fact72
f72 <- get_pca_ind(fact72)
f72$coord
data72 <- ESS7 %>% 
  filter(oute >-1 & race > -1 & ins > -1) 
data72$factor <- f72$coord[,1]
summary(data72$factor)




### ESS 8 variables

# imdfetn -> high values = "Allow fewer migrants of different race"
table(ESS8$imdfetn)
ESS8$race <- ESS8$imdfetn 
ESS8$race[ESS8$race == 7 | ESS8$race == 8 | ESS8$race == 9] <- NA
table(ESS8$race)

# impcntr -> high values = "Allow fewer immigrants from outside Europe"
table(ESS8$impcntr)
ESS8$oute <- ESS8$impcntr 
ESS8$oute[ESS8$oute == 7 | ESS8$oute == 8 | ESS8$oute == 9] <- NA
table(ESS8$oute)

# imwbcnt -> high values = "Immigrants make country better"
table(ESS8$imwbcnt)
ESS8$mig <- ESS8$imwbcnt 
ESS8$mig[ESS8$mig == 77 | ESS8$mig == 88 | ESS8$mig == 99] <- NA
table(ESS8$mig)

# imueclt -> high values = "Cultural life enriched by migrants"
table(ESS8$imueclt)
ESS8$cul <- ESS8$imueclt
ESS8$cul[ESS8$cul == 77 | ESS8$cul == 88 | ESS8$cul == 99] <- NA
table(ESS8$cul)

# imbgeco -> high values = "Immigration good for economy"
table(ESS8$imbgeco)
ESS8$eco <- ESS8$imbgeco
ESS8$eco[ESS8$eco == 77 | ESS8$eco == 88 | ESS8$eco == 99] <- NA
table(ESS8$eco)

# imsmetn -> high values = allow fewer migrants of majority ethnicity
table(ESS8$imsmetn)
ESS8$ins <- ESS8$imsmetn
ESS8$ins[ESS8$ins == 7 | ESS8$ins == 8 | ESS8$ins == 9] <- NA
table(ESS8$ins)

# delete NAs for work hours
ESS8$wkhtot[ESS8$wkhtot == 666 | ESS8$wkhtot == 777 | ESS8$wkhtot == 888 | ESS8$wkhtot == 999] <- NA
ESS8$wkhtot <- as.numeric(ESS8$wkhtot)
table(ESS8$wkhtot)

# delete NAs for people are helpful
ESS8$pplhlp[ESS8$pplhlp == 77 | ESS8$pplhlp == 88 | ESS8$pplhlp == 99] <- NA
ESS8$pplhlp <- as.numeric(ESS8$pplhlp)
table(ESS8$pplhlp)

# delete NAs for state of education
ESS8$stfedu[ESS8$stfedu == 77 | ESS8$stfedu == 88 | ESS8$stfedu == 99] <- NA
ESS8$stfedu <- as.numeric(ESS8$stfedu)
table(ESS8$stfedu)

# delete NAs for state of health services
ESS8$stfhlth[ESS8$stfhlth == 77 | ESS8$stfhlth == 88 | ESS8$stfhlth == 99] <- NA
ESS8$stfhlth <- as.numeric(ESS8$stfhlth)
table(ESS8$stfhlth)

# delete NAs for gayrights
ESS8$freehms[ESS8$freehms == 7 | ESS8$freehms == 8 | ESS8$freehms == 9] <- NA
ESS8$freehms <- as.numeric(ESS8$freehms)
table(ESS8$freehms)



# covariates

# education
ESS8$education <- NA
ESS8$education[ESS8$edulvlb < 120] <- "Primary"
ESS8$education[ESS8$edulvlb > 120 & ESS8$edulvlb < 500] <- "Secondary"
ESS8$education[ESS8$edulvlb >500 & ESS8$edulvlb < 1000] <- "Tertiary"

#gndr
ESS8$gndr[ESS8$gndr > 2] <- NA

#agea
ESS8$agea[ESS8$agea > 110] <- NA

#foreignparent
ESS8$foreignparent <- NA
ESS8$foreignparent[ESS8$mocntr == 2 | ESS8$facntr == 2] <- "Foreign Parent"
ESS8$foreignparent[ESS8$mocntr == 1 & ESS8$facntr == 1] <- "Two Native Parents"

#workstat
ESS8$workstat <- NA
ESS8$workstat[ESS8$mainact == 1] <- "Paid Work"
ESS8$workstat[ESS8$mainact == 2] <- "Education"
ESS8$workstat[ESS8$mainact == 3 | ESS8$mainact == 4] <- "Unemployed"
ESS8$workstat[ESS8$mainact == 6] <- "Retired"
ESS8$workstat[ESS8$mainact == 5 | ESS8$mainact == 7 | ESS8$mainact == 8 | ESS8$mainact == 9] <- "Housework or Other"



### create factors ###
fact81 <- prcomp(~ESS8$mig + ESS8$cul + ESS8$eco)
fact81
f81 <- get_pca_ind(fact81)
f81$coord
data81 <- ESS8 %>% 
  filter(mig >-1 & eco > -1 & cul > -1) 
data81$factor <- -1 * f81$coord[,1]
summary(data81$factor)

fact82 <- prcomp(~ESS8$oute + ESS8$race + ESS8$ins)
fact82
f82 <- get_pca_ind(fact82)
f82$coord
data82 <- ESS8 %>% 
  filter(oute >-1 & race > -1 & ins > -1) 
data82$factor <- -1 * f82$coord[,1]
summary(data82$factor)





### ESS 9 variables ###

# imdfetn -> high values = "Allow fewer migrants of different race"
table(ESS9$imdfetn)
ESS9$race <- ESS9$imdfetn 
ESS9$race[ESS9$race == 7 | ESS9$race == 8 | ESS9$race == 9] <- NA
table(ESS9$race)

# impcntr -> high values = "Allow fewer immigrants from outside Europe"
table(ESS9$impcntr)
ESS9$oute <- ESS9$impcntr 
ESS9$oute[ESS9$oute == 7 | ESS9$oute == 8 | ESS9$oute == 9] <- NA
table(ESS9$oute)

# imwbcnt -> high values = "Immigrants make country better"
table(ESS9$imwbcnt)
ESS9$mig <- ESS9$imwbcnt 
ESS9$mig[ESS9$mig == 77 | ESS9$mig == 88 | ESS9$mig == 99] <- NA
table(ESS9$mig)

# imueclt -> high values = "Cultural life enriched by migrants"
table(ESS9$imueclt)
ESS9$cul <- ESS9$imueclt
ESS9$cul[ESS9$cul == 77 | ESS9$cul == 88 | ESS9$cul == 99] <- NA
table(ESS9$cul)

# imbgeco -> high values = "Immigration good for economy"
table(ESS9$imbgeco)
ESS9$eco <- ESS9$imbgeco
ESS9$eco[ESS9$eco == 77 | ESS9$eco == 88 | ESS9$eco == 99] <- NA
table(ESS9$eco)

# imsmetn -> high values = allow fewer migrants of majority ethnicity
table(ESS9$imsmetn)
ESS9$ins <- ESS9$imsmetn
ESS9$ins[ESS9$ins == 7 | ESS9$ins == 8 | ESS9$ins == 9] <- NA
table(ESS9$ins)

# delete NAs for work hours
ESS9$wkhtot[ESS9$wkhtot == 666 | ESS9$wkhtot == 777 | ESS9$wkhtot == 888 | ESS9$wkhtot == 999] <- NA
ESS9$wkhtot <- as.numeric(ESS9$wkhtot)
table(ESS9$wkhtot)

# delete NAs for state of education
ESS9$stfedu[ESS9$stfedu == 77 | ESS9$stfedu == 88 | ESS9$stfedu == 99] <- NA
ESS9$stfedu <- as.numeric(ESS9$stfedu)
table(ESS9$stfedu)

# delete NAs for state of health services
ESS9$stfhlth[ESS9$stfhlth == 77 | ESS9$stfhlth == 88 | ESS9$stfhlth == 99] <- NA
ESS9$stfhlth <- as.numeric(ESS9$stfhlth)
table(ESS9$stfhlth)

# delete NAs for gayrights
ESS9$freehms[ESS9$freehms == 7 | ESS9$freehms == 8 | ESS9$freehms == 9] <- NA
ESS9$freehms <- as.numeric(ESS9$freehms)
table(ESS9$freehms)

# lrscale 0 = left, 10 = right
table(ESS9$lrscale)
ESS9$lrscale[ESS9$lrscale == 77 | ESS9$lrscale == 88 | ESS9$lrscale == 99] <- NA
table(ESS9$lrscale)


# covariates

# education
ESS9$education <- NA
ESS9$education[ESS9$edulvlb < 120] <- "Primary"
ESS9$education[ESS9$edulvlb > 120 & ESS9$edulvlb < 500] <- "Secondary"
ESS9$education[ESS9$edulvlb >500 & ESS9$edulvlb < 1000] <- "Tertiary"

#gndr
ESS9$gndr[ESS9$gndr > 2] <- NA

#agea
ESS9$agea[ESS9$agea > 110] <- NA

#foreignparent
ESS9$foreignparent <- NA
ESS9$foreignparent[ESS9$mocntr == 2 | ESS9$facntr == 2] <- "Foreign Parent"
ESS9$foreignparent[ESS9$mocntr == 1 & ESS9$facntr == 1] <- "Two Native Parents"

#workstat
ESS9$workstat <- NA
ESS9$workstat[ESS9$mainact == 1] <- "Paid Work"
ESS9$workstat[ESS9$mainact == 2] <- "Education"
ESS9$workstat[ESS9$mainact == 3 | ESS9$mainact == 4] <- "Unemployed"
ESS9$workstat[ESS9$mainact == 6] <- "Retired"
ESS9$workstat[ESS9$mainact == 5 | ESS9$mainact == 7 | ESS9$mainact == 8 | ESS9$mainact == 9] <- "Housework or Other"


### create factors ###
fact91 <- prcomp(~ESS9$mig + ESS9$cul + ESS9$eco)
fact91
f91 <- get_pca_ind(fact91)
f91$coord
data91 <- ESS9 %>% 
  filter(mig >-1 & eco > -1 & cul > -1) 
data91$factor <- f91$coord[,1]
summary(data91$factor)

fact92 <- prcomp(~ESS9$oute + ESS9$race + ESS9$ins)
fact92
f92 <- get_pca_ind(fact92)
f92$coord
data92 <- ESS9 %>% 
  filter(oute >-1 & race > -1 & ins > -1) 
data92$factor <- -1 * f92$coord[,1]
summary(data92$factor)





### ESS 10 variables ###

# imwbcnt -> high values = "Immigrants make country better"

table(ESS10$imwbcnt)

ESS10$mig <- as.numeric(ESS10$imwbcnt) 

ESS10$mig[ESS10$mig == 77 | ESS10$mig == 88 | ESS10$mig == 99] <- NA

table(ESS10$mig)

# impcntr -> high values = "Allow fewer immigrants from outside Europe"

table(ESS10$impcntr)

ESS10$oute <- as.numeric(ESS10$impcntr) 

ESS10$oute[ESS10$oute == 7 | ESS10$oute == 8 | ESS10$oute == 9] <- NA

table(ESS10$oute)

# imdfetn -> high values = "Allow fewer migrants of different race"

table(ESS10$imdfetn)

ESS10$race <- as.numeric(ESS10$imdfetn) 

ESS10$race[ESS10$race == 7 | ESS10$race == 8 | ESS10$race == 9] <- NA

table(ESS10$race)

# imueclt -> high values = "Cultural life enriched by migrants"

table(ESS10$imueclt)

ESS10$cul <- as.numeric(ESS10$imueclt)

ESS10$cul[ESS10$cul == 77 | ESS10$cul == 88 | ESS10$cul == 99] <- NA

table(ESS10$cul)

# imbgeco -> high values = "Immigration good for economy"

table(ESS10$imbgeco)

ESS10$eco <- as.numeric(ESS10$imbgeco)

ESS10$eco[ESS10$eco == 77 | ESS10$eco == 88 | ESS10$eco == 99] <- NA

table(ESS10$eco)

# imsmetn -> high values = allow fewer migrants of majority ethnicity
table(ESS10$imsmetn)

ESS10$ins <- ESS10$imsmetn


# stfdem -> high values = "satisfied with democy in own country"

table(ESS10$stfdem)

ESS10$dem <- as.numeric(ESS10$stfdem)

ESS10$dem[ESS10$dem == 77 | ESS10$dem == 88 | ESS10$dem == 99] <- NA

table(ESS10$dem)

# freehms high values = anti gay rights

table(ESS10$freehms)
ESS10$freehms[ESS10$freehms == 7 | ESS10$freehms == 8 | ESS10$freehms == 9] <- NA
ESS10$freehms <- as.numeric(ESS10$freehms)
table(ESS10$freehms)

# happy high values = happiers
table(ESS10$happy)
ESS10$happy[ESS10$happy == 77 | ESS10$happy == 88 | ESS10$happy == 99] <- NA
ESS10$happy <- as.numeric(ESS10$happy)
table(ESS10$happy)

# delete NAs for work hours
ESS10$wkhtot[ESS10$wkhtot == 666 | ESS10$wkhtot == 777 | ESS10$wkhtot == 888 | ESS10$wkhtot == 999] <- NA
ESS10$wkhtot <- as.numeric(ESS10$wkhtot)
table(ESS10$wkhtot)


# trstsci high values = more trust
table(ESS10$trstsci)
ESS10$trstsci[ESS10$trstsci == 77 | ESS10$trstsci == 88 | ESS10$trstsci == 99] <- NA
ESS10$trstsci <- as.numeric(ESS10$trstsci)
table(ESS10$trstsci)

# trstplc high values = more trust
table(ESS10$trstplc)
ESS10$trstplc[ESS10$trstplc == 77 | ESS10$trstplc == 88 | ESS10$trstplc == 99] <- NA
ESS10$trstplc <- as.numeric(ESS10$trstplc)
table(ESS10$trstplc)

# trstep high values = more trust
table(ESS10$trstep)
ESS10$trstep[ESS10$trstep == 77 | ESS10$trstep == 88 | ESS10$trstep == 99] <- NA
ESS10$trstep <- as.numeric(ESS10$trstep)
table(ESS10$trstep)

# polintr low values = high interest
table(ESS10$polintr)
ESS10$polintr[ESS10$polintr == 7 | ESS10$polintr == 8 | ESS10$polintr == 9] <- NA
ESS10$polintr <- as.numeric(ESS10$polintr)
table(ESS10$polintr)

# lrscale 0 = left, 10 = right
table(ESS10$lrscale)
ESS10$lrscale[ESS10$lrscale == 77 | ESS10$lrscale == 88 | ESS10$lrscale == 99] <- NA
ESS10$lrscale <- as.numeric(ESS10$lrscale)
table(ESS10$lrscale)

# eisced higher values = higher education
table(ESS10$eisced)
ESS10$eisced[ESS10$eisced == 55 | ESS10$eisced == 77 | ESS10$eisced == 88 | ESS10$eisced == 99] <- NA
ESS10$eisced <- as.numeric(ESS10$eisced)
table(ESS10$eisced)

# wkhtot high values = more hours worked
table(ESS10$wkhtot)
ESS10$wkhtot[ESS10$wkhtot == 666 | ESS10$wkhtot == 777 | ESS10$wkhtot == 888 | ESS10$wkhtot == 999] <- NA
ESS10$wkhtot <- as.numeric(ESS10$wkhtot)
table(ESS10$wkhtot)


# covariates

# education
ESS10$education <- NA
ESS10$education[ESS10$edulvlb < 120] <- "Primary"
ESS10$education[ESS10$edulvlb > 120 & ESS10$edulvlb < 500] <- "Secondary"
ESS10$education[ESS10$edulvlb >500 & ESS10$edulvlb < 1000] <- "Tertiary"

#gndr
ESS10$gndr[ESS10$gndr > 2] <- NA

#agea
ESS10$agea[ESS10$agea > 110] <- NA

#foreignparent
ESS10$foreignparent <- NA
ESS10$foreignparent[ESS10$mocntr == 2 | ESS10$facntr == 2] <- "Foreign Parent"
ESS10$foreignparent[ESS10$mocntr == 1 & ESS10$facntr == 1] <- "Two Native Parents"

#workstat
ESS10$workstat <- NA
ESS10$workstat[ESS10$mainact == 1] <- "Paid Work"
ESS10$workstat[ESS10$mainact == 2] <- "Education"
ESS10$workstat[ESS10$mainact == 3 | ESS10$mainact == 4] <- "Unemployed"
ESS10$workstat[ESS10$mainact == 6] <- "Retired"
ESS10$workstat[ESS10$mainact == 5 | ESS10$mainact == 7 | ESS10$mainact == 8 | ESS10$mainact == 9] <- "Housework or Other"


### create factors ###
fact101 <- prcomp(~ESS10$mig + ESS10$cul + ESS10$eco)
fact101
f101 <- get_pca_ind(fact101)
f101$coord
data101 <- ESS10 %>% 
  filter(mig >-1 & eco > -1 & cul > -1) 
data101$factor <- f101$coord[,1]
summary(data101$factor)

fact102 <- prcomp(~ESS10$oute + ESS10$race + ESS10$ins)
fact102
f102 <- get_pca_ind(fact102)
f102$coord
data102 <- ESS10 %>% 
  filter(oute >-1 & race > -1 & ins > -1) 
data102$factor <- f102$coord[,1]
summary(data102$factor)
